package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 实时数据-汇总manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class BossRealTimeManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	NewUserByDayManager newUserByDayManager;
	@Autowired
	NewUserByHourManager newUserByHourManager;
	
	@Autowired
	DauByDayManager dauByDayManager;
	@Autowired
	DauByHourManager dauByHourManager;
	
	@Autowired
	AddCashUserByHourManager addCashUserByHourManager;
	
	@Autowired
	AddCashUserByDayManager addCashUserByDayManager;
	
	@Autowired
	CashAddByDayManager cashAddByDayManager;
	@Autowired
	CashAddByHourManager cashAddByHourManager;
	
	@Autowired
	NewUserAddCashUserByHourManager newUserAddCashUserByHourManager;
	
	@Autowired
	NewUserCashAddByHourManager newUserCashAddByHourManager;
	
	@Autowired
	UserSecondLeftByDayManager userSecondLeftByDayManager;

	@Autowired
	OnlineByQuantumManager onlineByQuantumManager;
	
	@Autowired
	private ConfigManager configManager;
	public String getMoneyType() {
		return configManager.getConfigValueByName(ConfigManager.MONEY_TYPE) == null ? "($)" : configManager.getConfigValueByName(ConfigManager.MONEY_TYPE);
	}

	public static final String IOSZB_INDEX = "ioszb"; // ios正版标识
	public static final String YYB_INDEX = "yyb"; // 应用宝标识
	public static final String YHLM_INDEX = "yhlm"; // 混服标识
	public static final String HF_INDEX = "hf"; // 硬核联盟标识
	public static final String IOSZB = "*202*";  // ios正版渠道号
	public static final String YYB = "*283*";  // 应用宝渠道号
	public static final String YHLM = "*113*,*213*,*223*,*233*,*243*,*253*,*313*";  // 硬核联盟渠道号
	@SuppressWarnings({ "serial" })
	public static final Map<String, String> INDEX2NAME = new HashMap<String, String>() {
		{
            put("all", "全部平台");
			put(IOSZB_INDEX, "ios正版");
			put(YYB_INDEX, "应用宝");
			put(YHLM_INDEX, "硬核联盟");
			put(HF_INDEX, "混服");
		}
	};
	
	// 得到实时汇总数据
	public List<BossRealTimeData> getBossRealTimeData(Date d) {
		List<BossRealTimeData> result = new ArrayList<>();
		
		// 新增账号数
		BossRealTimeData o1 = new BossRealTimeData();
		o1.setTitle("新增账号数");
		// 活跃账号数
		BossRealTimeData o2 = new BossRealTimeData();
		o2.setTitle("活跃账号数");
		// 付费账号数
		BossRealTimeData o3 = new BossRealTimeData();
		o3.setTitle("付费账号数");
		// 付费金额=
		BossRealTimeData o4 = new BossRealTimeData();
		o4.setTitle("付费金额" + getMoneyType());
		// 活跃付费渗透率
		BossRealTimeData o5 = new BossRealTimeData();
		o5.setTitle("活跃付费率");
		// 付费ARPU
		BossRealTimeData o6 = new BossRealTimeData();
		o6.setTitle("付费ARPU" + getMoneyType());
		// 昨日新增帐号次日留存率
		BossRealTimeData o7 = new BossRealTimeData();
		o7.setTitle("昨日新增帐号次日留存率");

		o1.setValue(newUserByDayManager.getAllNewUserByConditions(d, "0", "0", "0"));
		o2.setValue(dauByDayManager.getAllActiveUserByConditions(d, "0", "0", "0"));
		o3.setValue(addCashUserByDayManager.getAllPayUserByConditions(d, "0", "0", "0"));
		o4.setValue(cashAddByDayManager.getAllCashAddByConditions(d, "0", "0", "0"));
		o7.setValue(userSecondLeftByDayManager.getAllUserSecondLeftByConditions(d, "0", "0", "0"));
		
		if (!"-".equals(o3.getValue()) && !"-".equals(o2.getValue()) && !"0".equals(o2.getValue())) {
			o5.setValue(PageHelper.double2Percent(Integer.parseInt(o3.getValue()) * 1.0 / Integer.parseInt(o2.getValue())));
		} 

		if (!"-".equals(o3.getValue()) && !"-".equals(o4.getValue()) && !"0".equals(o3.getValue())) {
			o6.setValue(PageHelper.floatKeepTwoNumber((float)(Integer.parseInt(o4.getValue()) * 1.0 / Integer.parseInt(o3.getValue()))) + "");
		} 
		
		result.add(o1);
		result.add(o2);
		result.add(o3);
		result.add(o4);
		result.add(o5);
		result.add(o6);
		result.add(o7);
		
		return result;
	}
	
	// 得到今日实时详细数据
	public List<BossRealTimeDetailData> getBossRealTimeDetailData(Date d) {
        Map<String, BossRealTimeDetailData> tmp = new HashMap<>();

        List<Object[]> tmp1 = onlineByQuantumManager.getAllOnlineRealTimeDatas(d);
        if (tmp1 != null && tmp1.size() > 0 && tmp1.get(0) != null) {
            for (Object[] e : tmp1) {
                String serverid = e[0].toString();
                int onlinenum = Integer.parseInt(e[1].toString());
                String serverType = BiPlatformManager.getServerType(serverid);
                BossRealTimeDetailData t = tmp.get(serverid);
                if (t == null) {
                    t = new BossRealTimeDetailData();
                    t.setServerid(serverid);
                    t.setItem(serverType);
                    tmp.put(serverid, t);
                }
                t.setOnlinenum(onlinenum);
            }
        }

        // 实时活跃
        List<Object[]> tmp2 = dauByDayManager.getAllDauRealTimeDatas(d);
        if (tmp2 != null && tmp2.size() > 0 && tmp2.get(0) != null) {
            for (Object[] e : tmp2) {
                String serverid = e[0].toString();
                int dau = Integer.parseInt(e[1].toString());
                String serverType = BiPlatformManager.getServerType(serverid);
                BossRealTimeDetailData t = tmp.get(serverid);
                if (t == null) {
                    t = new BossRealTimeDetailData();
                    t.setServerid(serverid);
                    t.setItem(serverType);
                    tmp.put(serverid, t);
                }
                t.setActivenum(dau);
            }
        }

        return new ArrayList<>(tmp.values());
	}

    public long getMaxUpdateTime() {
       /* List<EntityOnlineByQuantum> os = onlineByQuantumManager.findByQuery(0, 1, "from EntityOnlineByQuantum where day = '" + Time.getDateStr(Time.currentDate()) + "' and quantum <= '" + Time.getHHmm() + "' order by day desc ,quantum desc ");
        if (os != null && os.size() > 0 && os.get(0) != null) {  // 如果数据库中有记录，则返回记录时间
            EntityOnlineByQuantum o = os.get(0);
            return o.getDay().getTime() + Time.convertHourMin2Long(o.getQuantum());
        } else {  // 如果数据库中没有记录，则取当前时间
            return System.currentTimeMillis();
        }*/

        return System.currentTimeMillis();
    }
	
	// 实时汇总数据
	public class BossRealTimeData {
		private String title;  // 标题
		private String value = "-";  // 数值

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			if (value != null && !"".equals(value)) {  // 为空时，不赋值，默认为“-”
				this.value = value;
			} 
		}
	}
	
	// 实时对比数据类和chart类
	public class BossRealTimeDetailData {
		private String serverid;
		@ChartFlow(name = "服务器名称")
		private String servername;
		private String channelid;
		@ChartSelect(name = "在线人数", key = 1)
		@ChartData1(name = "在线人数", key = 1)
		private int onlinenum;
		private int activenum;

		private String item;  // all=全部平台;ioszb=ios正版;hf=混服;yyb=应用宝;yhlm=硬核联盟
		private String itemName;

        // 根据onlinenum排序的比较器
        public final Comparator<BossRealTimeDetailData> comparatorOnline = new Comparator<BossRealTimeDetailData>() {
            public int compare(BossRealTimeDetailData s1, BossRealTimeDetailData s2) {
                return s2.getOnlinenum() - s1.getOnlinenum();
            }
        };

        // 根据onlinenum排序reverse的比较器
        public final Comparator<BossRealTimeDetailData> comparatorOnlineReverse = new Comparator<BossRealTimeDetailData>() {
            public int compare(BossRealTimeDetailData s1, BossRealTimeDetailData s2) {
                return s1.getOnlinenum() - s2.getOnlinenum();
            }
        };

		public String getServerid() {
			return serverid;
		}

		public void setServerid(String serverid) {
            this.serverid = serverid;
            this.setServername(BiPlatformManager.getServerNameById(serverid));
		}

		public String getServername() {
			return servername;
		}

		public void setServername(String servername) {
			this.servername = servername;
		}

		public String getChannelid() {
			return channelid;
		}

		public void setChannelid(String channelid) {
			this.channelid = channelid;

			if (contains(IOSZB, channelid)) {
				setItem(IOSZB_INDEX);
			} else if (contains(YYB, channelid)) {
                setItem(YYB_INDEX);
			} else if (contains(YHLM, channelid)) {
                setItem(YHLM_INDEX);
			} else {
                setItem(HF_INDEX);
			}
		}

		private boolean contains(String pstr, String channelid) {
            return pstr.contains("*" + channelid + "*");
		}

		public String getItem() {
			return item;
		}

		public void setItem(String item) {
			this.item = item;
			itemName = INDEX2NAME.get(item);
		}

		public int getOnlinenum() {
			return onlinenum;
		}

		public void setOnlinenum(int onlinenum) {
			this.onlinenum = onlinenum;
		}

		public int getActivenum() {
			return activenum;
		}

		public void setActivenum(int activenum) {
			this.activenum = activenum;
		}

		public String getItemName() {
			return itemName;
		}

		public void setItemName(String itemName) {
			this.itemName = itemName;
		}
	}
}
